package com.fypg.module.medical.service.outpatientorderguide;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.fypg.module.medical.controller.admin.outpatientorderguide.vo.*;
import com.fypg.module.medical.dal.dataobject.outpatientorderguide.OutpatientOrderGuideDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.outpatientorderguide.OutpatientOrderGuideMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.framework.common.util.collection.CollectionUtils.diffList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 门诊订单候诊信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OutpatientOrderGuideServiceImpl implements OutpatientOrderGuideService {

    @Resource
    private OutpatientOrderGuideMapper outpatientOrderGuideMapper;

    @Override
    public String createOutpatientOrderGuide(OutpatientOrderGuideSaveReqVO createReqVO) {
        // 插入
        OutpatientOrderGuideDO outpatientOrderGuide = BeanUtils.toBean(createReqVO, OutpatientOrderGuideDO.class);
        outpatientOrderGuideMapper.insert(outpatientOrderGuide);
        // 返回
        return outpatientOrderGuide.getId();
    }

    @Override
    public void updateOutpatientOrderGuide(OutpatientOrderGuideSaveReqVO updateReqVO) {
        // 校验存在
        validateOutpatientOrderGuideExists(updateReqVO.getId());
        // 更新
        OutpatientOrderGuideDO updateObj = BeanUtils.toBean(updateReqVO, OutpatientOrderGuideDO.class);
        outpatientOrderGuideMapper.updateById(updateObj);
    }

    @Override
    public void deleteOutpatientOrderGuide(String id) {
        // 校验存在
        validateOutpatientOrderGuideExists(id);
        // 删除
        outpatientOrderGuideMapper.deleteById(id);
    }

    @Override
        public void deleteOutpatientOrderGuideListByIds(List<String> ids) {
        // 校验存在
        validateOutpatientOrderGuideExists(ids);
        // 删除
        outpatientOrderGuideMapper.deleteByIds(ids);
        }

    private void validateOutpatientOrderGuideExists(List<String> ids) {
        List<OutpatientOrderGuideDO> list = outpatientOrderGuideMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OUTPATIENT_ORDER_GUIDE_NOT_EXISTS);
        }
    }

    private void validateOutpatientOrderGuideExists(String id) {
        if (outpatientOrderGuideMapper.selectById(id) == null) {
            throw exception(OUTPATIENT_ORDER_GUIDE_NOT_EXISTS);
        }
    }

    @Override
    public OutpatientOrderGuideDO getOutpatientOrderGuide(String id) {
        return outpatientOrderGuideMapper.selectById(id);
    }

    @Override
    public PageResult<OutpatientOrderGuideDO> getOutpatientOrderGuidePage(OutpatientOrderGuidePageReqVO pageReqVO) {
        return outpatientOrderGuideMapper.selectPage(pageReqVO);
    }

}